//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Average.h"
using namespace Cephei::QL::Instruments;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Instruments::CAverage::CAverage (boost::shared_ptr<QuantLib::Average>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAverage = NULL;
#endif
	_ppAverage = &childNative;
    
}
Cephei::QL::Instruments::CAverage::CAverage (QuantLib::Average& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAverage = NULL;
#endif
	_ppAverage = new boost::shared_ptr<QuantLib::Average> (&childNative);
    
    _AverageOwner = owner;
    
}

Cephei::QL::Instruments::CAverage::CAverage (CAverage^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAverage = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAverage = new boost::shared_ptr<QuantLib::Average> (copy->GetShared());
        
    }
}
Cephei::QL::Instruments::CAverage::CAverage (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAverage = NULL;
#endif
	if (!t->IsSubclassOf(CAverage::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CAverage::CAverage (QuantLib::Handle<QuantLib::Average>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAverage = &childNative;
	_ppAverage = &static_cast<boost::shared_ptr<QuantLib::Average>>(childNative.currentLink());
    
    _AverageOwner = owner;
}
Cephei::QL::Instruments::CAverage::CAverage (QuantLib::Handle<QuantLib::Average> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAverage = &childNative;
	_ppAverage = &static_cast<boost::shared_ptr<QuantLib::Average>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CAverage::CAverage (QuantLib::Average childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAverage = NULL;
#endif
	_ppAverage = new boost::shared_ptr<QuantLib::Average> (new QuantLib::Average (childNative));
    
}
#endif

Cephei::QL::Instruments::CAverage::~CAverage ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAverage != NULL)
    {
	    delete _ppAverage;
        _ppAverage = NULL;
    }
}
Cephei::QL::Instruments::CAverage::!CAverage ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAverage != NULL)
    {
	    delete _ppAverage;
    }
}
QuantLib::Average& Cephei::QL::Instruments::CAverage::GetReference ()
{
    if (_ppAverage == NULL) throw gcnew NativeNullException ();
	return **_ppAverage;
}
boost::shared_ptr<QuantLib::Average>& Cephei::QL::Instruments::CAverage::GetShared ()
{
    if (_ppAverage == NULL) throw gcnew NativeNullException ();
	return *_ppAverage;
}
QuantLib::Average* Cephei::QL::Instruments::CAverage::GetPointer ()
{
    if (_ppAverage == NULL) throw gcnew NativeNullException ();
	return &**_ppAverage;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Average>& Cephei::QL::Instruments::CAverage::GetHandle ()
{
	if (_phAverage == NULL)
	{
		_phAverage = new Handle<QuantLib::Average> (*_ppAverage);
	}
	return *_phAverage;
}
#endif
bool Cephei::QL::Instruments::CAverage::HasNative () 
{
	return (_ppAverage != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

